package com.todo.delicacy.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.enums.EnableTypeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.delicacy.application.backend.service.RestaurantInfoBackendService;
import com.todo.delicacy.application.convert.FoodConvert;
import com.todo.delicacy.application.service.RestaurantInfoService;
import com.todo.delicacy.iface.backend.request.*;
import com.todo.delicacy.iface.backend.response.QueryRestaurantInfoByConditionResp;
import com.todo.delicacy.iface.backend.response.RestaurantDetailInfoResp;
import com.todo.delicacy.application.entity.bo.RestaurantDetailBO;
import com.todo.delicacy.application.entity.bo.RestaurantInfoBO;
import com.todo.delicacy.application.entity.condition.RestaurantInfoConditionBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (RestaurantInfo)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:48
 */
@Slf4j
@Service("restaurantInfoBackendService")
public class RestaurantInfoBackendServiceImpl implements RestaurantInfoBackendService {

    @Resource
    private RestaurantInfoService restaurantInfoService;


    @Override
    public BizResponse<Boolean> insertRestaurantInfo(InsertRestaurantInfoReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        RestaurantInfoBO restaurantInfoBO = FoodConvert.convertToRestaurantInfoBO(req);
        Boolean insertResult = restaurantInfoService.insertRestaurantInfo(restaurantInfoBO);
        log.info("RestaurantInfoBackendService#insertRestaurantInfo 新增餐馆信息完成 入参：【{}】，出参：【{}】", JSONObject.toJSONString(restaurantInfoBO), insertResult);
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> updateRestaurantInfoById(UpdateRestaurantInfoReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        RestaurantInfoBO restaurantInfoBO = FoodConvert.convertToRestaurantInfoBO(req);
        Boolean updateResult = restaurantInfoService.updateRestaurantInfo(restaurantInfoBO);
        log.info("RestaurantInfoBackendService#updateRestaurantInfoById 更新餐馆信息完成 入参：【{}】，出参：【{}】", JSONObject.toJSONString(restaurantInfoBO), updateResult);
        return BizResponse.success(updateResult);
    }

    @Override
    public BizResponse<Boolean> enableRestaurantInfo(EnableRestaurantInfoReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        Boolean enableResult = restaurantInfoService.updateRestaurantOpenStatus(req.getRestaurantId(), EnableTypeEnum.getByType(req.getEnable()));
        log.info("RestaurantInfoBackendService#enableRestaurantInfo 启用禁用餐馆信息完成 入参：【{}】，出参：【{}】", JSONObject.toJSONString(req), enableResult);
        return BizResponse.success(enableResult);
    }

    @Override
    public BizPageResponse<QueryRestaurantInfoByConditionResp> queryRestaurantInfoByCondition(QueryRestaurantInfoByConditionReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        RestaurantInfoConditionBO conditionBO = FoodConvert.convertToRestaurantInfoConditionBO(req);
        PageResult<RestaurantInfoBO> result = restaurantInfoService.queryRestaurantInfoByCondition(conditionBO);
        if (result == null) {
            log.error("RestaurantInfoBackendService#queryRestaurantInfoByCondition 查询餐馆信息完成，数据为空 入参：【{}】", JSONObject.toJSONString(req));
            return BizPageResponse.success();
        }
        return BizPageResponse.success(FoodConvert.convertToQueryRestaurantInfoByConditionRespPage(result));
    }

    @Override
    public BizResponse<RestaurantDetailInfoResp> queryRestaurantInfoById(QueryRestaurantInfoByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        RestaurantDetailBO restaurantDetailBO = restaurantInfoService.queryRestaurantDetailById(req.getId());
        log.info("RestaurantInfoBackendService#queryRestaurantInfoById 查询餐馆详情完成 入参：【{}】，出参：【{}】", JSONObject.toJSONString(req), JSONObject.toJSONString(restaurantDetailBO));
        return BizResponse.success(FoodConvert.convertToRestaurantDetailInfoResp(restaurantDetailBO));
    }
}
